package com.ycxy.school.express.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ycxy.school.express.pojo.dto.UserAddNewDTO;
import com.ycxy.school.express.pojo.dto.UserLoginDTO;
import com.ycxy.school.express.pojo.dto.UserUpdateDTO;
import com.ycxy.school.express.pojo.vo.UserListItemVO;
import com.ycxy.school.express.pojo.vo.UserStandardVO;
import com.ycxy.school.express.security.LoginPrincipal;
import com.ycxy.school.express.service.IUserService;
import com.ycxy.school.express.web.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

@Slf4j
@Api(tags = "01. 用户管理模块")
@Validated
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    IUserService userService;

    public UserController() {
        log.info("创建控制器对象：userController");
    }

    // http://localhost:9080/users/login
    @ApiOperation("用户登录")
    @ApiOperationSupport(order = 50)
    @PostMapping("/login")
    public JsonResult<String> login(UserLoginDTO userLoginDTO) {
        log.debug("开始处理【用户登录】的请求，参数：{}", userLoginDTO);
        String jwt = userService.login(userLoginDTO);
        return JsonResult.ok(jwt);
    }

    // http://localhost:9080/users/add-new?username=aa&phone=bb&email=cc
    @ApiOperation("添加用户")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult<Void> addNew(UserAddNewDTO userAddNewDTO) {
        log.debug("开始处理【添加用户】的请求，参数：{}", userAddNewDTO);
        userService.addNew(userAddNewDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/users/9527/delete
    @ApiOperation("根据id删除用户")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult<Void> delete(@Range(min = 1, message = "删除用户失败，尝试删除的用户的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【根据id删除用户】的请求，参数：{}", id);
        userService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/users/9527/update
    @ApiOperation("根据id修改用户")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult<Void> updateById(@PathVariable Long id, UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户详情】的请求，参数ID：{}, 新数据：{}", id, userUpdateDTO);
        userService.updateInfoById(id, userUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/users/login-update
    @ApiOperation("修改当前登录的用户信息")
    @ApiOperationSupport(order = 310)
    @PostMapping("/login-update")
    public JsonResult<Void> updateLoginUserDetails(UserUpdateDTO userUpdateDTO) {
        log.debug("开始处理【修改用户详情】的请求,新数据：{}", userUpdateDTO);
        userService.updateLoginUserDetails(userUpdateDTO);
        return JsonResult.ok();
    }

    // http://localhost:9080/users/9527/enable
    @ApiOperation("启用用户")
    @ApiOperationSupport(order = 320)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/enable")
    public JsonResult<Void> setEnable(@Range(min = 1, message = "启用用户失败，尝试启用的用户的ID无效！")
                                   @PathVariable Long id) {
        log.debug("开始处理【启用用户】的请求，参数：{}", id);
        userService.setEnable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/users/9527/disable
    @ApiOperation("禁用用户")
    @ApiOperationSupport(order = 321)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @PostMapping("/{id:[0-9]+}/disable")
    public JsonResult<Void> setDisable(
            @Range(min = 1, message = "禁用用户失败，尝试禁用的用户的ID无效！")
            @PathVariable Long id) {
        log.debug("开始处理【禁用用户】的请求，参数：{}", id);
        userService.setDisable(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/users
    @ApiOperation("查询用户列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<UserListItemVO>> list(
            @ApiIgnore @AuthenticationPrincipal LoginPrincipal loginPrincipal) {
        log.debug("开始处理【查询用户列表】的请求，无参数");
        log.debug("当前登录的当事人：{}", loginPrincipal);
        List<UserListItemVO> list = userService.list();
        return JsonResult.ok(list);
    }

    // http://localhost:9080/users/9527/details
    @ApiOperation("根据id查询用户详情")
    @ApiOperationSupport(order = 430)
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "long")
    @GetMapping("/{id:[0-9]+}/details")
    public JsonResult<UserStandardVO> getUserDetails(
            @Range(min = 1, message = "查询用户详情失败，尝试查询的用户的ID无效！")
            @PathVariable Long id) {
        log.debug("开始处理【查询用户详情】的请求，参数ID：{}, 新数据：{}", id);
        UserStandardVO userDetails = userService.getUserDetails(id);
        return JsonResult.ok(userDetails);
    }

    // http://localhost:9080/users/9527/login-details
    @ApiOperation("查询当前登录用户详情")
    @ApiOperationSupport(order = 440)
    @GetMapping("/login-details")
    public JsonResult<UserStandardVO> getLoginUserDetails() {
        UserStandardVO userDetails = userService.getLoginUserDetails();
        return JsonResult.ok(userDetails);
    }

}
